<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>computeUnitOfWork</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="../../libs/babel.min.js"></script>
    <script src="../../libs/react-17.0.2.js"></script>
    <script src="../../libs/react-dom-17.0.2.js"></script>
    <script type="text/babel">
      const Child = React.forwardRef(function Child(props, ref) {
        const inputRef = React.useRef()
        React.useImperativeHandle(ref, () => ({
          focus: () => {
            inputRef.current.focus()
          },
          a() {
            console.log(inputRef)
          },
        }))
        return (
          <>
            <h1>h1</h1>
            <input ref={inputRef} />
          </>
        )
      })

      const Child1 = React.forwardRef(function Profile(props, ref) {
        React.useEffect(() => {
          console.log(ref) //arg => this.titleRef = arg
        }, [])
        return (
          <>
            <h1>h1</h1>
            <h2 ref={ref}>h2</h2>
          </>
        )
      })

      const App = props => {
        const [color, setColor] = React.useState("red")
        // dom ref
        const domRef = React.useRef()
        // component ref
        const compRef = React.useRef()
        // component ref
        const comp2Ref = React.useRef()
        // function ref
        const funcRef = React.useRef()

        const onUpdate = () => {
          console.log("domRef = ", domRef)
          console.log("compRef = ", compRef)
          console.log("funcRef = ", funcRef)
          console.log("comp2Ref = ", comp2Ref)
          setColor(color === "red" ? "green" : "red")
        }

        React.useEffect(() => {
          debugger
          funcRef.current = 2
        }, [])

        return (
          <div>
            <button onClick={onUpdate}>切换颜色</button>
            <div className="is-23" ref={domRef} style={{ color: color }}>
              is-23
            </div>
            <Child ref={compRef} />
            {color === "red" && <Child1 ref={comp2Ref} />}
          </div>
        )
      }
      const root = document.getElementById("root")

      ReactDOM.render(<App app={1} />, root)
      console.dir(root)
    </script>
  </body>
</html>
